package com.quicklife.service.impl;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.quicklife.pojo.ActionLogs;
import com.quicklife.pojo.Business;
import com.quicklife.pojo.Coupons;
import com.quicklife.service.inter.FileUploadService;
import com.quicklife.service.inter.WebCouponManageService;
import com.quicklife.util.JsonUtil;
import com.quicklife.util.SqlHelper;
import com.quicklife.util.StringUtil;

/**
 * 商家优惠管理
 * 
 * @author HackerD
 * 
 */
public class WebCouponManageServiceImpl implements WebCouponManageService {

	@Override
	public String getCoupons(JSONObject jsonObject) {
		List list = null;
		try {
			String busid = jsonObject.getString("busid");
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");

			list = SqlHelper
					.executeQuery(
							"select d.id,d.name,d.type,d.busname,d.context,d.area,d.pub_time,d.beg_time,d.end_time,d.image,"
									+ "d.uses,d.score,d.status from (select rownum rnum,a.* from (select t.id,t.name,t.type,"
									+ "b.name busname,context,area,pub_time,beg_time,end_time,image,uses,t.score,t.status "
									+ "from coupons t inner join business b on t.busid=b.id "
									+ "where t.busid=? order by pub_time desc) a) d where rnum>=? and rnum<?",
							new String[] { busid, StringUtil.toString(begin),
									StringUtil.toString((begin + need)) },
							Coupons.class);

			String rtInfos = JsonUtil.createRtJsonStr(
					StringUtil.RESULT_CODE_OK, list);
			jsonObject = JSONObject.fromObject(rtInfos);

			list = SqlHelper.executeQuery(
					"select t.id from coupons t where t.busid=?",
					new String[] { busid }, Coupons.class);
			if (list!=null&&list.size() > 0) {
				jsonObject.put("total", list.size());
			} else {
				jsonObject.put("total", 0);
			}

			return jsonObject.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String getCouponComms(JSONObject jsonObject) {
		List list = null;
		try {
			int id = jsonObject.getInt("cpid");
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");

			list = SqlHelper
					.executeQuery(
							"select * from (select rownum rnum,f.* from (select b.nickname,c.name "
									+ "username,d.name busname,e.name actionname,AC_TIME,a.COMMENTS,"
									+ "SCORE,a.PHOTO,RECORDING,VIDEO from action_logs a  "
									+ "inner join tuser b on a.userid=b.id "
									+ "inner join business c on a.bussid=c.id "
									+ "inner join coupons d on a.couponid=d.id "
									+ "inner join action_type e on a.ac_type=e.id "
									+ "where  a.ac_type=3 and a.couponid=? "
									+ "order by ac_time) f) "
									+ "where rnum>=? and rnum<?",
							new String[] { StringUtil.toString(id),
									StringUtil.toString(begin),
									StringUtil.toString((begin + need)) },
							ActionLogs.class);

			String rtInfos = JsonUtil.createRtJsonStr(
					StringUtil.RESULT_CODE_OK, list);

			jsonObject = JSONObject.fromObject(rtInfos);

			list = SqlHelper.executeQuery("select a.id from action_logs a  "
					+ "where  a.ac_type=3 and a.couponid=? ",
					new String[] { StringUtil.toString(id) }, ActionLogs.class);

			if (list.size() > 0) {
				jsonObject.put("total", list.size());
			} else {
				jsonObject.put("total", 0);
			}

			return jsonObject.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String addCoupon(HttpServletRequest request) {
		try {
			FileUploadService fileUploadService = new FileUploadServiceImpl();
			String tempInfos = fileUploadService.getUploadInfos(request);
			JSONObject jsonObject = JSONObject.fromObject(tempInfos);
			String id = request.getParameter("busid");

			String sql = "insert into coupons(id,name,type,busid,context,area,beg_time,end_time,image) "
					+ "values(?,?,?,?,?,?,?,?,?)";
			int tableId = SqlHelper.getTableId("coupons");
			String beg_time = jsonObject.getString("beg_time");
			String end_time = jsonObject.getString("end_time");
			System.out.println(beg_time);
			Object[] params = new Object[] { tableId,
					jsonObject.getString("name"), jsonObject.getString("type"),
					id, jsonObject.getString("context"),
					jsonObject.getString("area"),
					StringUtil.stringToSqlDate(beg_time),
					StringUtil.stringToSqlDate(end_time),
					jsonObject.getString("image") };

			// 更新资料
			SqlHelper.executeUpdate(sql, params);

			// 保存image
			fileUploadService.saveFile();

			List list = SqlHelper
					.executeQuery(
							"select a.id,a.name,a.type,b.name busname,context,area,pub_time,beg_time,end_time,image,uses,a.score,a.status"
									+ " from coupons a inner join business b on a.busid=b.id where a.id=? order by pub_time desc ",
							new String[] { StringUtil.toString(tableId) },
							Coupons.class);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String mdfConponInfos(HttpServletRequest request) {
		try {
			FileUploadService fileUploadService = new FileUploadServiceImpl();
			String tempInfos = fileUploadService.getUploadInfos(request);
			JSONObject jsonObject = JSONObject.fromObject(tempInfos);
			String sql = "update coupons set name=?,type=?,context=?,area=?,beg_time=?,end_time=?,status=? where id=?";
			Object[] params = new Object[] {
					jsonObject.getString("name"),
					jsonObject.getString("type"),
					jsonObject.getString("context"),
					jsonObject.getString("area"),
					StringUtil
							.stringToSqlDate(jsonObject.getString("beg_time")),
					StringUtil
							.stringToSqlDate(jsonObject.getString("end_time")),
					jsonObject.getString("status"),
					request.getParameter("cpid") };

			// 判断如果logo为空的话 不更新
			if (jsonObject.containsKey("image")) {
				sql = "update coupons set name=?,type=?,context=?,area=?,beg_time=?,end_time=?,image=?,status=? where id=?";
				params = new Object[] {
						jsonObject.getString("name"),
						jsonObject.getString("type"),
						jsonObject.getString("context"),
						jsonObject.getString("area"),
						StringUtil.stringToSqlDate(jsonObject
								.getString("beg_time")),
						StringUtil.stringToSqlDate(jsonObject
								.getString("end_time")),
						jsonObject.getString("image"),
						jsonObject.getString("status"),
						request.getParameter("cpid") };
			}

			// 更新资料
			SqlHelper.executeUpdate(sql, params);

			// 保存Logo
			fileUploadService.saveFile();

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK,
					StringUtil.RESULT_SUCESS_UPDATE);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String deleteCoupon(HttpServletRequest request) {
		try {
			String cpid = request.getParameter("cpid");
			String sql = "delete from coupons where id=?";

			SqlHelper.executeUpdate(sql, new String[] { cpid });

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, null);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

}
